package com.appfahrt.map;

import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.MapView.Projection;
import org.osmdroid.views.overlay.MyLocationOverlay;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.util.Log;

public class MyCircuitOverlay extends MyLocationOverlay {

	private String TAG = MyCircuitOverlay.class.getSimpleName();
	private MapView mapView;
	private Paint circlePainter;
	private Point screenCurrentPoint;
	private int meters;
	private GeoPoint myLocation;

	public MyCircuitOverlay(Context context, MapView mapView, GeoPoint pGeoPoint) {
		super(context, mapView);
		Log.e(TAG,"Konstruktor MyOwnLocationOverlay");
		this.mapView = mapView;
		Log.e(TAG,"pGeoPoint " + pGeoPoint);
		this.myLocation = pGeoPoint;
	}

	// This method is used to get user submitted radius from our application
	public void setMeters(int meters) {
		Log.e(TAG,"setMeters "+meters);
		this.meters = meters;
	}

	public boolean drawOwn(Canvas canvas, MapView mapView,
			boolean shadow) {
		// Set the painter to paint our circle. setColor = blue, setAlpha = 70
		// so the background
		// can still be seen. Feel free to change these settings
		circlePainter = new Paint();
		circlePainter.setAntiAlias(true);
		circlePainter.setStrokeWidth(30.0f);
		circlePainter.setColor(0x618652);
		circlePainter.setStyle(Style.FILL_AND_STROKE);
		circlePainter.setAlpha(70);

		// Get projection from the mapView.
		Projection projection = mapView.getProjection();
		// Get current location
		screenCurrentPoint = new Point();
		// Project the gps coordinate to screen coordinate
		projection.toPixels(myLocation, screenCurrentPoint);

		int radius = metersToRadius(myLocation.getLatitudeE6() / 1000000);
		// draw the blue circle
		canvas.drawCircle(screenCurrentPoint.x, screenCurrentPoint.y, radius,
				circlePainter);
//		super.draw(canvas, mapView, shadow);
		return true;
	}

	// hack to get more accurate radius, because the accuracy is changing as the
	// location
	// getting further away from the equator
	public int metersToRadius(double latitude) {
		return (int) (mapView.getProjection().metersToEquatorPixels(meters) * (1 / Math
				.cos(Math.toRadians(latitude))));
	}
	@Override
	public void draw(final Canvas c, final MapView osmv, final boolean shadow) {
		drawOwn(c, osmv, shadow);
	}
}